package com.echo.echovividlive.listener;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.echo.echovividlive.domain.entity.Comment;
import com.echo.echovividlive.domain.entity.UserInteract;
import com.echo.echovividlive.domain.entity.Video;
import com.echo.echovividlive.mapper.CommentMapper;
import com.echo.echovividlive.mapper.UserInteractMapper;
import com.echo.echovividlive.mapper.VideoMapper;
import com.echo.echovividlive.service.ICommentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.echo.echovividlive.common.Constants.*;

@Slf4j
@Component
@RequiredArgsConstructor
public class RabbitMQListener {
    private final CommentMapper commentMapper;

    private final StringRedisTemplate stringRedisTemplate;

    private final UserInteractMapper userInteractMapper;

    private final FileStorageService fileStorageService;
    // 点赞队列
    @RabbitListener(queues = USER_LIKE_QUEUE)
    public void updateUserLikeInfo(Integer userId) throws InterruptedException {
        Thread.sleep(256L);
        log.info("处理用户{}的点赞消息...",userId);
        List<String> likes = stringRedisTemplate.opsForList().range(USER_LIKE_KEY + userId, 0, -1);
        String likesListStr = StringUtils.join(likes, ",");
        UserInteract userInteract = userInteractMapper.selectById(userId);
        if (userInteract != null) {
            userInteract.setVideoIds(likesListStr);
            userInteractMapper.updateById(userInteract);
            return;
        }
        userInteract = new UserInteract();
        userInteract.setUserId(userId);
        userInteract.setVideoIds(likesListStr);
        userInteractMapper.insertOrUpdate(userInteract);
    }


    // 粉丝队列
    @RabbitListener(queues = USER_FANS_QUEUE)
    public void updateUserFansInfo(Integer userId) throws InterruptedException {
        Thread.sleep(256L);
        log.info("处理用户{}的粉丝消息...",userId);
        List<String> likes = stringRedisTemplate.opsForList().range(USER_FANS_KEY + userId, 0, -1);
        String fansIds = StringUtils.join(likes, ",");
        UserInteract userInteract = userInteractMapper.selectById(userId);
        if (userInteract != null) {
            userInteract.setFansIds(fansIds);
            userInteractMapper.updateById(userInteract);
            return;
        }
        userInteract = new UserInteract();
        userInteract.setUserId(userId);
        userInteract.setFansIds(fansIds);
        userInteractMapper.insertOrUpdate(userInteract);
    }

    // 关注队列
    @RabbitListener(queues = USER_FOLLOWING_QUEUE)
    public void updateUserFollowingInfo(Integer userId) throws InterruptedException {
        Thread.sleep(256L);
        log.info("处理用户{}的关注消息...",userId);
        List<String> likes = stringRedisTemplate.opsForList().range(USER_FOLLOWING_KEY + userId, 0, -1);
        String followingStr = StringUtils.join(likes, ",");
        UserInteract userInteract = userInteractMapper.selectById(userId);
        if (userInteract != null) {
            userInteract.setFollowingIds(followingStr);
            userInteractMapper.updateById(userInteract);
            return;
        }
        userInteract = new UserInteract();
        userInteract.setUserId(userId);
        userInteract.setFollowingIds(followingStr);
        userInteractMapper.insertOrUpdate(userInteract);
    }

    // 视频删除队列
    @RabbitListener(queues = VIDEO_DELETE_QUEUE)
    public void updateUserFollowingInfo(String video) throws InterruptedException {
        Thread.sleep(256L);
        Video bean = JSONObject.parseObject(video, Video.class);
        log.info("处理视频Id为{}的删除消息...",bean.getVideoId());
        String fileName = bean.getUrl().substring(bean.getUrl().lastIndexOf("/") + 1);
        String cover = bean.getCover().substring(bean.getCover().lastIndexOf("/") + 1);
        // 删除视频和封面
        FileInfo fileInfo = new FileInfo()
                .setPlatform("minio-video")
                .setFilename(fileName);
        FileInfo fileInfo2 = new FileInfo()
                .setPlatform("minio-video")
                .setFilename(cover);
        fileStorageService.delete(fileInfo);
        fileStorageService.delete(fileInfo2);
        // 删除评论
        commentMapper.delete(new LambdaQueryWrapper<Comment>().eq(Comment::getVideoId, bean.getVideoId()));
    }
}
